package com.xuecheng.checkcode.controller;

import com.xuecheng.checkcode.model.CheckCodeParamsDto;
import com.xuecheng.checkcode.model.CheckCodeResultDto;
import com.xuecheng.checkcode.model.CodekeyDto;
import com.xuecheng.checkcode.service.CheckCodeService;
import com.xuecheng.checkcode.sms.component.SmsComponent;
import com.xuecheng.checkcode.utils.CheckUtil;
import com.xuecheng.checkcode.utils.Constant;
import com.xuecheng.checkcode.utils.RandomUtil;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Mr.M
 * @version 1.0
 * @description 验证码服务接口
 * @date 2022/9/29 18:39
 */
@Api(value = "验证码服务接口")
@RestController
public class CheckCodeController {

    @Resource(name = "PicCheckCodeService")
    private CheckCodeService picCheckCodeService;

    @Autowired
    SmsComponent smsComponent ;


    @Autowired
    private RedisTemplate<String,String> redisTemplate ;


    @ApiOperation(value="生成验证信息", notes="生成验证信息")
    @PostMapping(value = "/pic")
    public CheckCodeResultDto generatePicCheckCode(CheckCodeParamsDto checkCodeParamsDto){
        return picCheckCodeService.generate(checkCodeParamsDto);
    }

    @ApiOperation(value="校验", notes="校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "业务名称", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "key", value = "验证key", required = true, dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", paramType="query")
    })
    @PostMapping(value = "/verify")
    public Boolean verify(String key, String code){
        Boolean isSuccess = picCheckCodeService.verify(key,code);
        return isSuccess;
    }

    @PostMapping("phone")
    public CodekeyDto sendCode(@Param("param1") String param1){
        String code = redisTemplate.opsForValue().get(Constant.REDIS_CODE_FREFX +param1);
        CodekeyDto codekeyDto = new CodekeyDto();
        codekeyDto.setKey(code);
        if(!StringUtils.isEmpty(code)){
            return codekeyDto;
        }
        code = RandomUtil.getSixBitRandom();
        boolean checkPhone = CheckUtil.checkPhone(param1);
        //手机号正确
        if(checkPhone){
//            发送验证码
            sendPhoneCode(param1,code);
            codekeyDto.setKey(code);
            return codekeyDto ;
        }

        boolean checkEmail = CheckUtil.checkEmailFormat(param1);
        if(checkEmail){
            sendEmailCode(param1,code);
            codekeyDto.setKey(code);
            return codekeyDto ;
        }
        throw new RuntimeException("输入错误，请重新输入");
//        return false;
    }

    //TODO 邮箱发送验证码
    private boolean sendEmailCode(@Param("param1") String param1,@Param("code")String code) {
        redisTemplate.opsForValue().set(Constant.REDIS_CODE_FREFX + param1, code, 3, TimeUnit.MINUTES);
        return true ;
    }

//    发送手机验证码
    private boolean sendPhoneCode(@Param("param1") String param1,@Param("code")String code) {

//        String code = redisTemplate.opsForValue().get(ConstantCodeFrefx+param1);
//        if(!StringUtils.isEmpty(code)){
//            return true ;
//        }
        //        如果获取不到，生成验证码，
//        code = RandomUtil.getSixBitRandom();
        try {
//            发送验证码
//            smsComponent.sendSmsCode(param1, code);
            redisTemplate.opsForValue().set(Constant.REDIS_CODE_FREFX + param1, code, 3, TimeUnit.MINUTES);
            System.err.println(code);
            return true ;
        }catch (Exception e){
            return false;
        }
    }
}
